package com.kamfu.bio.client;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @program: kamfu
 * @ClassName BioFileClient
 * @description:
 * @author: liandy
 * @create: 2021-12-30 14:16
 * @Version 1.0
 **/
public class BioFileClient {
    public static void main(String[] args) {
        try {
            //1.创建文件对象
            File file=new File("F:\\kamfu\\src\\main\\resource\\1.jpg");
            //2.将文件对象包装成文件输入流
            FileInputStream fileInputStream=new FileInputStream(file);
            //3.与服务端建立连接
            Socket socket=new Socket("localhost",9000);
            //4.获取字节输出流
            OutputStream outputStream = socket.getOutputStream();
            //5.将字节输出流包装成Data输出流
            DataOutputStream dataOutputStream=new DataOutputStream(outputStream);
            //将内存数据写出
            dataOutputStream.writeUTF(".jpg");
            //6.将文件读到缓存字节数组
            byte[] buf=new byte[2014];
            int len;
            while((len=fileInputStream.read(buf))>0){
                dataOutputStream.write(buf,0,len);
            }
            dataOutputStream.flush();
            socket.shutdownOutput();
            System.out.println("文件传输成功");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}